<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">
                首页</el-breadcrumb-item
            >
            <el-breadcrumb-item :to="{ path: '/notice/List' }">
                通知公告</el-breadcrumb-item
            >
            <el-breadcrumb-item>编辑通知公告</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card class="box-card">
            <el-page-header @back="goBack" content="编辑通知公告">
            </el-page-header>
            <el-form
                :model="editForm"
                :rules="editFormRules"
                ref="editFormRef"
                label-width="120px"
                hide-required-asterisk
            >
                <el-form-item label="标题" prop="title">
                    <el-input v-model="editForm.title" placeholder="请输入标题">
                    </el-input>
                </el-form-item>

                <el-form-item label="内容" prop="content">
                    <el-input
                        type="textarea"
                        autosize
                        v-model="editForm.content"
                        placeholder="请输入内容"
                    >
                    </el-input>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input
                        v-model="editForm.sort"
                        placeholder="请输入排序"
                    />
                </el-form-item>
                <el-form-item label="状态" align="left">
                    <el-switch
                        v-model="editForm.status"
                        :active-value="1"
                        :inactive-value="0"
                    ></el-switch>
                </el-form-item>
                <el-form-item align="left">
                    <el-button type="primary" @click="subEdit">保存</el-button>
                    <el-button @click="resetForm">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>

<script>
import requestApis from '@/request'
export default {
    data () {
        return {
            editForm: {
                id: '',
                title: '',
                content: '',
                sort: 10,
                status: 1
            },
            // 添加表单验证规则对象 tag value_type
            editFormRules: {
                title: [
                    { required: true, message: '请输入通知标题', trigger: 'change' }
                ],
                content: [
                    { required: true, message: '请输入通知内容', trigger: 'blur' }
                ]
            }
        }
    },
    created () {
        this.getRow(this.$route.query.id)
    },
    methods: {
        getRow (id) {
            requestApis.getNoticeRow({ id }).then(res => {
                if (res.meta.code === 1) {
                    this.editForm.id = res.data.id
                    this.editForm.title = res.data.title
                    this.editForm.content = res.data.content
                    this.editForm.sort = res.data.sort
                    this.editForm.status = res.data.status
                } else {
                    this.$message({
                        message: res.meta.msg,
                        type: 'warning'
                    })
                    this.$router.back()
                }
            }).catch(error => {
                console.log(error)
            })
        },
        goBack () {
            this.$router.back()
        },
        subEdit () {
            this.$refs.editFormRef.validate(valid => {
                if (!valid) {
                    return false
                }
                requestApis.saveNoticeEdit(this.editForm).then(res => {
                    if (res.meta.code === 1) {
                        this.$message({
                            message: res.meta.msg,
                            type: 'success'
                        })
                        this.$router.back()
                    } else {
                        this.$message({
                            message: res.meta.msg,
                            type: 'warning'
                        })
                    }
                }).catch(error => {
                    console.log(error)
                })
            })
        },
        resetForm () {
            this.$refs.editFormRef.resetFields()
            this.editForm.status = 1
        }
    }
}
</script>

<style>
.el-form {
    width: 60%;
    margin-top: 20px;
}
</style>
